package name.williamwall.buxoid;
/**
 * This software is made available under the Gnu General Public License v3
 * and has absolutely no waranty. You may change the software to suit your needs.
 * 
 * Author: William Wall, IV
 */
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditBudget extends Activity {
	public static final String BUDGET_ID = "budgetId";
	private BuxoidDb mDb;
	private Long mBudgetId;
	private EditText mLimit;
	private AutoCompleteTextView mTag;
	
	@Override
	protected void onCreate(Bundle state) {
		super.onCreate(state);
		mDb = new BuxoidDb(this);
		mDb.open();
		
		setTitle("Set a Budget:");
		setContentView(R.layout.edit_budget);
		
		mBudgetId = state != null ? state.getLong(BUDGET_ID): null;
		
		if (mBudgetId == null) {
			Bundle extras = getIntent().getExtras();
			mBudgetId = extras != null ? extras.getLong(BUDGET_ID): null;
		}
		
		mTag = (AutoCompleteTextView) findViewById(R.id.edit_budget_tag);
		mLimit = (EditText) findViewById(R.id.edit_budget_limit);
		
		Button cancel = (Button) findViewById(R.id.edit_budget_cancel);
		Button save = (Button) findViewById(R.id.edit_budget_save);
		
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				mDb.close();
				finish();
			}
		});
		
		save.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				String text = mLimit.getText().toString();
				
				if (text.length() > 0) {
					mDb.saveBudget(mTag.getText().toString(), Double.parseDouble(text));
				}
				mDb.close();
				Toast.makeText(EditBudget.this, "Budget saved.", Toast.LENGTH_SHORT).show();
				finish();
			}
		});
		
//		Log.d("Buxfer", "editing budget " + mBudgetId);
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}
	
	private void populateFields() {
		Cursor c = mDb.fetchAllParentTags();
		startManagingCursor(c);
		
		// if the cursor is empty or otherwise unusable, tell the user and exit
		if (c.getCount() <= 0 || !c.moveToFirst()) {
			Toast.makeText(this, "You have no tags with which to make budgets", Toast.LENGTH_SHORT).show();
			mDb.close();
			finish();
		}
		
		mTag.setAdapter(new TagListAdapter(this, c, mDb, true));
		
		if (mBudgetId != null && mBudgetId > 0) {
			Cursor cb = mDb.fetchTag(mBudgetId);
			startManagingCursor(cb);
			if (cb.getCount() > 0 && cb.moveToFirst()) {
				mTag.setText(cb.getString(cb.getColumnIndex(BuxoidDb.TAG_NAME)));
				mLimit.setText(cb.getString(cb.getColumnIndex(BuxoidDb.TAG_LIMIT)));
			}
		}
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
//		Log.d("Buxfer", "restoring EditBudget state");
		super.onRestoreInstanceState(savedInstanceState);
		mBudgetId = savedInstanceState != null ? savedInstanceState.getLong(BUDGET_ID): null;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
//		Log.d("Buxfer", "saving EditBudget state");
		super.onSaveInstanceState(outState);
		if (mBudgetId != null) {
			outState.putLong(BUDGET_ID, mBudgetId);
		}
	}
}
